﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
namespace DAL
{
    public class AccessHelp
    {
        private static readonly string ConStr = ConfigurationManager.ConnectionStrings["Access"].ConnectionString;
        public OleDbTransaction Tran = null;
        public OleDbConnection conn = null;
        public OleDbCommand cmd = null;
        public void OpenTran()
        {
            conn = new OleDbConnection(ConStr);
            conn.Open();
            Tran = conn.BeginTransaction();
        }
        public void CommitTran()
        {
            Tran.Commit();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
        public void TranRollBack()
        {
            Tran.Rollback();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
        public int ExecuteNonQuery(string sql, params OleDbParameter[] pms)
        {
                cmd = new OleDbCommand(sql, conn);
                cmd.Transaction = Tran;
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                return cmd.ExecuteNonQuery();
        }
        /// <summary>
        /// 执行一个操作如：添加，删除，修改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecteNonQuery(string sql,params OleDbParameter[] pms)
        {
            using (OleDbConnection conn = new OleDbConnection(ConStr))
            {
                    
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                    {

                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }              
            }
        }
       
        /// <summary>
        /// 执行一个查询返回第一行的第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql ,params OleDbParameter[] pms)
        {
            using (OleDbConnection conn = new OleDbConnection(ConStr))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql,conn))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static DataTable ExecuteReader(string sql, params OleDbParameter[] pms)
        {
            using (OleDbConnection conn = new OleDbConnection(ConStr))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql,conn))
                {
                    if (pms !=null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    conn.Open();
                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    dataAdapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }
    }
}
